package com.wokeMy.view;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wokeMy.view.Lottery.DoubleBallActivity;
import com.wokeMy.view.Lottery.HappyBallActivity;
import com.wokeMy.view.Lottery.OpenLottActivity;
import com.wokeMy.view.Lottery.RecordBetActivity;
import com.wokeMy.view.Lottery.YingkActivity;
import com.wokeMy.view.adpter.GGaoLotterAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.DoubleLo;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class LotteryMainActivity extends BaseActivity implements View.OnClickListener{
    ListView lotteryLv;
    ImageView lotterymain_back;
    TextView record_lottmain_tv;
    ArrayList<DoubleLo> data;
    Dialog mydialog;
    GGaoLotterAdapter lotterAdapter;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_lottery_main);
    }

    @Override
    public void initActivity() {
         lotterAdapter=new GGaoLotterAdapter(getApplicationContext(),data);
        View headView = LayoutInflater.from(this).inflate(R.layout.head_lottery_infor, null);
        headView.findViewById(R.id.wl_ball).setOnClickListener(this);
        headView.findViewById(R.id.base_ball).setOnClickListener(this);
        headView.findViewById(R.id.footm_ball).setOnClickListener(this);
        headView.findViewById(R.id.night_ball).setOnClickListener(this);
        headView.findViewById(R.id.happy_ball).setOnClickListener(this);
        headView.findViewById(R.id.double_ball).setOnClickListener(this);
        RollPagerView mRollViewPager=(RollPagerView) headView.findViewById(R.id.mRollViewPager);
        mRollViewPager.setAdapter(new TestNomalAdapter());
        mRollViewPager.setHintView(new ColorPointHintView(this, Color.WHITE,Color.RED));
        lotteryLv.addHeaderView(headView);
        lotteryLv.setAdapter(lotterAdapter);
        lotterymain_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        record_lottmain_tv.setOnClickListener(this);
        lotteryLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Log.e("AdapterView",""+position);
                Intent intent= new Intent();
                intent.setClass(LotteryMainActivity.this, OpenLottActivity.class);
                if (position==1){
                    intent.putExtra("lotteryid","118");
                }else if (position==2){
                    intent.putExtra("lotteryid","106");
                } else if (position==3){
                    intent.putExtra("lotteryid","123");
                }
                startActivity(intent);
            }
        });
    }

    @Override
    public void initView() {
        lotteryLv=myFindViewById(R.id.opinfor_lottery);
        lotterymain_back=myFindViewById(R.id.lotterymain_back);
        record_lottmain_tv=myFindViewById(R.id.record_lottmain_tv);

    }

    @Override
    public void initData() {
        data=new ArrayList<>();
        mydialog= Util.createLoadingDialog(LotteryMainActivity.this);
        cpgg();
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId()) {
            case R.id.lotterymain_back:
                finish();
                break;
            case R.id.record_lottmain_tv:
                intent.setClass(LotteryMainActivity.this,RecordBetActivity.class);
                startActivity(intent);
                break;
//            case R.id.wl_ball:
//                break;
//            case R.id.base_ball:
//                break;
//            case R.id.footm_ball:
//                break;
            case R.id.night_ball:
                intent.setClass(LotteryMainActivity.this,YingkActivity.class);
                startActivity(intent);
                break;
            case R.id.happy_ball:
                intent.setClass(LotteryMainActivity.this,HappyBallActivity.class);
                startActivity(intent);
                break;
            case R.id.double_ball:
                intent.setClass(LotteryMainActivity.this,DoubleBallActivity.class);
                startActivity(intent);
                break;
            default:
                tishiToast("暂未开通，近期期待！");
                break;
        }
    }
    // 彩票开奖公告
    public void  cpgg(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        Log.e("彩票开奖公告url",Constant.CPGG_URL);
        Log.e("彩票开奖公告params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CPGG_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("彩票开奖公告result", result);
                    try {
                        Util.closeDialog(mydialog);
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            Gson gson = new Gson();
                            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
                            for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                DoubleLo doubleLo = gson.fromJson(jsonArray.getString(i1), DoubleLo.class);
                                // status -1新奖期可预售 0新奖期开期通知 2奖期期结通知  3奖期开始开奖通知 7奖期开奖完成通知
                                data.add(doubleLo);
                            }
                            Log.e("doubleLo.getStatus()",""+data.toString());
                            lotterAdapter.notifyDataSetChanged();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }

    private class TestNomalAdapter extends StaticPagerAdapter {
        private int[] imgs = {
                R.mipmap.img1,
                R.mipmap.img2,
        };

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return imgs.length;
        }
    }
}
